import base64
from ast import main
import json
import requests


class BaseLogin():
    def __init__(self):
        self.user_token=self.login_user()
        self.marchant_token=self.login_merchant()

    def _picture_recognition(self, b64_encode):
        """图片识别"""
        data = {"username": 10780220, "password": "Wjx423515", "typeid": 3, "image": b64_encode}
        result = json.loads(requests.post("http://api.ttshitu.com/predict", json=data).text)
        if result['success']:
            return result['data']['result']
        else:
            return result['message']

    def login_user(self):
        data = {
            'url': 'https://api.admin.merchant.java.crmeb.net/api/admin/merchant/login',
            'method': 'post',
            'headers': {
                'content-type': 'application/json',
            },
            'json': {
                'account': '18292417675',
                'pwd': '000000',
                'key': 'bb21941151446b2e00af89df13835b75',
                'code': 'lf3l',
                'captcha': {
                    'captchaVerification': '',
                    'secretKey': '',
                    'token': '',
                },
            },
        }
        token = requests.request(**data).json()['data']['token']
        return token

    def _get_merchant_key(self):
        data = {
            'method': 'get',
            'url': 'http://api.admin.merchant.java.crmeb.net/api/publicy/validate/code/get',
            'method': 'post',
            'headers': {
                'accept': 'application/json, text/plain, */*',
                'accept-encoding': 'zh-CN,zh;q=0.9,',
            }
        }
        response = requests.request(**data).json()
        key = response['data']['key']
        picture: str = data['data']['code'].split(",").decode()
        b64 = base64.b64encode(base64.b64decode(picture)).decode()
        code = self._picture_recognition(b64)
        return {"key":key, "code":code}

    def login_merchant(self):
        data = {
            'method': 'post',
            'url':'https://api.admin.merchant.java.crmeb.net/api/admin/merchant/login',
            'headers' : {
                'content-type': 'application/json',
                },
            'json_data' : {
                'account': '18292417675',
                'pwd': '000000',
                **self._get_merchant_key(),
                'captcha': {
                    'captchaVerification': '',
                    'secretKey': '',
                    'token': '',
                },
            }
        }
        token = requests.request(**data).json()['data']['token']
        return token

    def platform_login(self):
        data = {
            'method': 'post',
            'url':'https://api.admin.merchant.java.crmeb.net/api/admin/platform/login',
            'headers' : {
                'content-type': 'application/json',
            },
            'json_data' : {
                'account': 'demo',
                'pwd': 'crmeb.com',
                'key': '91778410ac76902695a27a6987d423c6',
                'code': '87wm',
                'captcha': {
                    'captchaVerification': '',
                    'secretKey': '',
                    'token': '',
                },
            }
        }
        re=requests.request(**data).json()['data']['token']
        return re


if __name__ == '__main__':
    boj = BaseLogin()
    print(boj.user_login())